package io.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
//spring boot整合kafka https://blog.csdn.net/lt326030434/article/details/107182705/
@Component
public class KafkaProducerService {
    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;
    //private static final Logger log = LoggerFactory.getLogger(KafkaProducerService.class);

    public void sendMessage(String topic, Object object) {

        ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(topic, object);

        future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
            @Override
            public void onSuccess(SendResult<String, Object> result) {

                //log.info("发送消息成功: " + result.toString());
            }

            @Override
            public void onFailure(Throwable throwable) {

                //log.error("发送消息失败: " + throwable.getMessage());
            }
        });
    }
}
